//
//  WSSelectedPayTypeController.m
//  FD
//
//  Created by xiaofang wang on 2018/11/6.
//  Copyright © 2018年 ZhiYuan Network. All rights reserved.
//  选着支付方式
//  11-21 废弃

#import "WSSelectedPayTypeController.h"
#import "WSPayTypeSectionHeaderView.h"
#import "WSSelectBandCell.h"
#import "UnfoldFoldModel.h"


@interface WSSelectedPayTypeController ()<UITableViewDelegate,UITableViewDataSource>
/**tableView*/
@property (nonatomic, strong) UITableView *tv;

/**底部确认按钮*/
@property (nonatomic, strong) UIButton *sureBtn;
/**是否展开折叠model*/
@property (nonatomic, strong) UnfoldFoldModel *unfoldFoldModle;
/**banks*/
@property (nonatomic, strong) NSMutableArray *bankArr;

/**标记选择的支付方式*/
@property (nonatomic, copy) NSString *selectedPayTypeStr;

/**记录一个变量有更新按钮的文字 区头，
 怎么去改对应的区头的文字的文字。狗不着 怎么改？
 */
@property (nonatomic, copy) NSString *sectionHeaderViewBtnTitle;
@end

@implementation WSSelectedPayTypeController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = kMainLightGrayColor;
    self.navigationItem.title = [NSString stringWithFormat:@"%@:",NSLocalizedString(Order_PayType, @"支付方式:")];

    
    self.tv.frame = self.view.bounds;
    [self.view addSubview:self.tv];
    
    self.tv.tableFooterView = [UIView new];
    
    CGFloat y = SCREEN_HEIGHT - FD_TabbarSafeBottomMargin  -40;
    self.sureBtn.frame = CGRectMake(0, y , SCREEN_WIDTH, 40);
    [self.view addSubview:self.sureBtn];
    
    
    //创建展开折叠模型。
    self.unfoldFoldModle = [[UnfoldFoldModel alloc] init];
    self.unfoldFoldModle.isOpen = NO; //初始值
    
    self.bankArr = @[@"工商银行",@"建设银行",@"招商银行",@"宁波银行",@"农业银行",@"中国银行",@"建设银行"].mutableCopy;
}


- (NSMutableArray *)bankArr {
    if (!_bankArr) {
        _bankArr = [NSMutableArray array];
    }
    return _bankArr;
}

- (UITableView *)tv {
    if (!_tv) {
        _tv = [[UITableView alloc] initWithFrame:CGRectZero style:(UITableViewStyleGrouped)];
        _tv.backgroundColor = kMainLightGrayColor;
        //代理
        _tv.delegate = self;
        _tv.dataSource = self;
    
        //注册cell
        [_tv registerClass:[WSSelectBandCell class] forCellReuseIdentifier:@"WSSelectBandCell"];
        [_tv registerClass:[WSPayTypeSectionHeaderView class] forHeaderFooterViewReuseIdentifier:@"WSPayTypeSectionHeaderView"];
        [_tv registerClass:[WSPayTypeSectionHeaderOne class] forHeaderFooterViewReuseIdentifier:@"WSPayTypeSectionHeaderOne"];
    }
    return _tv;
}

- (UIButton *)sureBtn {
    if (!_sureBtn) {
        _sureBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [_sureBtn addTarget:self action:@selector(clickSureAction:) forControlEvents:(UIControlEventTouchUpInside)];
        
        [_sureBtn setTitle:@"确认" forState:(UIControlStateNormal)];
        [_sureBtn setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
        _sureBtn.backgroundColor = kMainRedColor;
    }
    return _sureBtn;
}

- (void)clickSureAction:(UIButton *)btn {
    FDLog(@"确认");
    
    FDLog(@"选择的是:%@",self.selectedPayTypeStr);
    
    if ([NSString isEmptyString:self.selectedPayTypeStr]) {
        [JRToast showWithText:NSLocalizedString(Order_SelectPayType, @"请选择支付方式")];

        return;
    }
    
    //回调支付方式 
    self.wsSelectedPayTypeBlock(self.selectedPayTypeStr);

    //直接返回
    [self.navigationController popViewControllerAnimated:YES];
}


#pragma mark - 代理
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (section == 0) {
        return 0;
    }else {
        if (self.unfoldFoldModle.isOpen) {
            return self.bankArr.count;
        }else {
            return 0;
        }
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    WSSelectBandCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WSSelectBandCell"];
//    cell.textLabel.text = self.dataArr[indexPath.row];
    if (indexPath.section == 0) {
        
    }else {
        //没赋上 值
        cell.bankLb.text  = self.bankArr[indexPath.row];
    }
    
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        WSPayTypeSectionHeaderOne *sectionOne = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"WSPayTypeSectionHeaderOne"];
        sectionOne.clickSelectDaoDianZhiFuBtnBlock = ^{
            //记录支付方式。
            self.selectedPayTypeStr = NSLocalizedString(L_Pay_Offline, @"线下支付");
        };
        return sectionOne;
        
    }else {
        WSPayTypeSectionHeaderView *sectionView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"WSPayTypeSectionHeaderView"];
        
        FDWeakSelf;
        sectionView.clickSelectBankBtnBlock = ^(UILabel * _Nonnull arrowLabel, UIButton * _Nonnull btn) {
            //取反
            weakSelf.unfoldFoldModle.isOpen = !weakSelf.unfoldFoldModle.isOpen;
            
            //刷新数据源
            NSIndexSet *set = [NSIndexSet indexSetWithIndex:section];
            [tableView reloadSections:set withRowAnimation:(UITableViewRowAnimationFade)];
        };

        return sectionView;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 44;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    self.selectedPayTypeStr = self.bankArr[indexPath.row];
    
    //更新 选择银行文字
    FDLog(@"选择的是:%@",self.selectedPayTypeStr);
    
    self.sectionHeaderViewBtnTitle = self.bankArr[indexPath.row]; //用于修改区头按钮文字
}


@end
